#pragma once
#include <bondlib/BondContainer.h>
#include <bondlib/SSSortInfo.h>
#include <uam/UserAccountManager.h>

#include <list>
#include <map>

#include "../Filter/FilterInterface.h"

class CBondFilter;
class CBrokerKeyList;
class RowRecordKeyMap {
  typedef std::map<int, BrokerKey> BondRowMap;
  BondRowMap m_screen_bonds;
  int m_max_row;

 public:
  RowRecordKeyMap();
  ~RowRecordKeyMap();
  void Clear();
  void Add(int row, const BrokerKey& bond);

  int GetRowCount();
  bool GetBondKey(int row, BrokerKey& bond);
};

class BestQuoteModel {  // 最优报价的数据模型
 protected:
  FilterCondition m_filter;         //
  CBrokerUnit m_broker;             //
  CBrokerKeyList* m_keys;           //
  CBondFilter* m_bond_filter;       //
  RowRecordKeyMap m_row_map;        // 是最近显示的一屏记录
  bool m_sort_attend;               // 关注债券要按照关注顺序排序
  std::set<int> m_expandedBond;     // 综合最优tab中，展开的债券
  std::vector<int> m_expandBroker;  // 展开的broker
 public:
  BestQuoteModel(const CBrokerUnit& stUnit, const CSortInfoManager& sortInfo);
  virtual ~BestQuoteModel();

  void SetSortAttend(bool sortAttend) { m_sort_attend = sortAttend; }
  const FilterCondition& GetFilter() const { return m_filter; }
  FilterCondition& GetFilter() { return m_filter; }
  CSortInfoManager& GetSortInfo();
  const CSortInfoManager& GetSortInfo() const;
  const CBrokerUnit& GetBroker() const { return m_broker; }
  CBrokerUnit& GetBroker() { return m_broker; }
  // 获取排序/更新后的记录的信息

  void GetRecordKeys(int row, int count, CBrokerKeyVector& keys);
  void GetScreenKeys(int row, int count, CBrokerKeyVector& keys);
  bool IsExist(const BrokerKey& key);
  int GetVersion(const BrokerKey& key);

  void ReloadRecords(FilterCondition& stTemp,
                     std::list<std::string>& stlBrokerID, bool bAllTab,
                     const QString& strGroupID);
  virtual int GetRowCount();
  virtual void ReloadRecordsBySearch(CBondContainer::FilterTypes type,
                                     const char* strSearch,
                                     const std::list<std::string>& brokers);
  virtual void ReloadRecordsByFilter(
      BLHCONDITION& blh, int nType, const char* szSearch,
      bool all);  // 加搜索混合筛选 2022.6 add by lance
  virtual bool CheckExistBestPricePush(
      BLHCONDITION& condStr, const std::list<xQBBestPriceUnit_c>& qblist);
  virtual bool UpdateBestPricePush(BLHCONDITION& condStr,
                                   const std::list<xQBBestPriceUnit_c>& qblist);
  virtual void Sort();

  void Update(const CBrokerKeyVector& addKeys,
              const CBrokerKeyVector& eraseKeys);
  void UpdateBrokerList();

  RowRecordKeyMap& GetRowMap();

 protected:
  virtual void GetRecordKeysImpl(int row, int count, CBrokerKeyVector& keys);
};